package com.holdtime.jtt809.taxi.module.web.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.holdtime.jtt809.taxi.module.entity.PlatformInfoDO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformInfoPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformInfoRespVO;
import com.holdtime.jtt809.taxi.module.model.vo.PlatformInfoSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.PlatformInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 下级平台信息")
@RestController
@RequestMapping("/taxi/platform-info")
@Validated
public class PlatformInfoController {

    @Resource
    private PlatformInfoService platformInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建下级平台信息")
    @PreAuthorize("@ss.hasPermission('taxi:platform-info:create')")
    public CommonResult<String> createPlatformInfo(@Valid @RequestBody PlatformInfoSaveReqVO createReqVO) {
        return success(platformInfoService.createPlatformInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新下级平台信息")
    @PreAuthorize("@ss.hasPermission('taxi:platform-info:update')")
    public CommonResult<Boolean> updatePlatformInfo(@Valid @RequestBody PlatformInfoSaveReqVO updateReqVO) {
        platformInfoService.updatePlatformInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除下级平台信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('taxi:platform-info:delete')")
    public CommonResult<Boolean> deletePlatformInfo(@RequestParam("id") String id) {
        platformInfoService.deletePlatformInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得下级平台信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('taxi:platform-info:query')")
    public CommonResult<PlatformInfoRespVO> getPlatformInfo(@RequestParam("id") String id) {
        PlatformInfoDO platformInfo = platformInfoService.getPlatformInfo(id);
        return success(BeanUtils.toBean(platformInfo, PlatformInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得下级平台信息分页")
    @PreAuthorize("@ss.hasPermission('taxi:platform-info:query')")
    public CommonResult<PageResult<PlatformInfoRespVO>> getPlatformInfoPage(@Valid PlatformInfoPageReqVO pageReqVO) {
        PageResult<PlatformInfoDO> pageResult = platformInfoService.getPlatformInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出下级平台信息 Excel")
    @PreAuthorize("@ss.hasPermission('taxi:platform-info:export')")
    public void exportPlatformInfoExcel(@Valid PlatformInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformInfoDO> list = platformInfoService.getPlatformInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "下级平台信息.xls", "数据", PlatformInfoRespVO.class,
                        BeanUtils.toBean(list, PlatformInfoRespVO.class));
    }

}